from fastapi import APIRouter, Request
from re_common.baselibrary.utils.core.mlamada import bools_string
from re_common.baselibrary.utils.core.requests_core import MsgCode

from apps.core.m_route import ContextIncludedRoute
from apps.core.return_info import ReturnInfo
from apps.crawler_platform.core_platform.g_model import InputPlatformModel, OtherInputMode, OtherArticleMode, \
    journalInputMode
from apps.crawler_platform.other_platform.other_article_down import OtherArticleDownStep
from apps.crawler_platform.other_platform.other_home import OtherHomeDownStep
from apps.crawler_platform.other_platform.other_list_down import OtherDownStep
from apps.crawler_platform.other_platform.other_sub_class_down import OtherSubClassDownStep

router = APIRouter(route_class=ContextIncludedRoute)


@router.post("/other/home")
async def otherhome(request: Request, input: InputPlatformModel[OtherInputMode]):
    """
    下载other库home
    :return:
    """
    return_info = ReturnInfo()
    down = OtherHomeDownStep(request, input)
    down.set_table(input)
    bools, err_info = await down.my_task()
    step_info = down.step_info
    return_info.status = bools_string(bools)
    if bools:
        return_info.msg_code = MsgCode.SUCCESS_CODE
        return_info.msg = step_info
        return_info.data = ""
    else:
        return_info.msg_code = 400
        return_info.msg = step_info
        return_info.data = {"err_info": err_info}

    return return_info.todict()


@router.post("/other/downlist")
async def otherlist(request: Request, input: InputPlatformModel[OtherInputMode]):
    """
    下载other库列表
    :return:
    """
    return_info = ReturnInfo()
    down = OtherDownStep(request, input)
    down.set_table(input)
    bools, err_info = await down.my_task()
    step_info = down.step_info
    return_info.status = bools_string(bools)
    if bools:
        return_info.msg_code = MsgCode.SUCCESS_CODE
        return_info.msg = step_info
        return_info.data = ""
    else:
        return_info.msg_code = 400
        return_info.msg = step_info
        return_info.data = {"err_info": err_info}

    return return_info.todict()


@router.post("/other/down_subclass")
async def othersucclasss(request: Request, input: InputPlatformModel[OtherInputMode]):
    """
    下载other库列表
    :return:
    """
    return_info = ReturnInfo()
    down = OtherSubClassDownStep(request, input)
    down.set_table(input)
    bools, err_info = await down.my_task()
    step_info = down.step_info
    return_info.status = bools_string(bools)
    if bools:
        return_info.msg_code = MsgCode.SUCCESS_CODE
        return_info.msg = step_info
        return_info.data = ""
    else:
        return_info.msg_code = 400
        return_info.msg = step_info
        return_info.data = {"err_info": err_info}

    return return_info.todict()


@router.post("/other/downarticle")
async def otherlist(request: Request, input: InputPlatformModel[OtherArticleMode]):
    """
    下载other库列表
    :return:
    """
    return_info = ReturnInfo()
    down = OtherArticleDownStep(request, input)
    down.set_table(input)
    bools, err_info = await down.my_task()
    step_info = down.step_info
    return_info.status = bools_string(bools)
    if bools:
        return_info.msg_code = MsgCode.SUCCESS_CODE
        return_info.msg = step_info
        return_info.data = ""
    else:
        return_info.msg_code = 400
        return_info.msg = step_info
        return_info.data = {"err_info": err_info}

    return return_info.todict()

# @router.post("/cnkilaw/get_cookie")
# async def cnkilaw_get_cookie(input: InputPlatformModel[journalInputMode]):
#     """
#         获取cnkilaw分页下载的cookie
#     """
#     return_info = ReturnInfo()
#     pa_list = list()
#     bools, proxies = await get_proxy("cnkilaw", "cnkilaw_list")
#     if bools:
#         headers = {
#             'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
#             "Host": "qikan.chaoxing.com",
#             'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36'
#
#         }
#         url = "https://qikan.chaoxing.com/mag/infos?mags=%s" % (journal_rawid)
#
#         rrq = RequestApiHelper.get_rrq()
#         kwargs = {
#             "rrq": rrq,
#             "header": headers,
#             "url": url,
#             "timeout": 30,
#             "proxy": proxies,
#             "moths": MRequest.GET,
#             "middler_list": [],
#             "allow_redirects": False
#         }
#         boolsg, dicts = await RequestApiHelper.etl_remote_meta(**kwargs)
#         if boolsg:
#             cookie1_str = rrq.resp.headers
#             cookie1_str_list = re.findall(rule1, str(cookie1_str))
#             for i in cookie1_str_list:
#                 pa = rule2.sub("", i)
#                 pa_list.append(pa)
#             cookie1 = ";".join(pa_list)
#             pa_list.clear()
#             headers["Host"] = "fxlogin.chaoxing.com"
#             headers["Cookie"] = "cookiecheck=true"
#             if "Location" in str(cookie1_str):
#                 url = cookie1_str['Location']
#             else:
#                 url = ""
#             rrq.set_url(url).set_sn(None) \
#                 .set_timeout(30).set_allow_redirects(False) \
#                 .set_proxy(proxies).set_header(headers).set_middler_list([])
#             boolsg2, dicts2 = await rrq.run(MRequest.GET)
#             if boolsg2:
#                 cookie2_str = rrq.resp.headers
#                 cookie2_str_list = re.findall(rule1, str(cookie2_str))
#                 for i in cookie2_str_list:
#                     if not i.startswith("JSESSIONID"):
#                         pa = rule2.sub("", i)
#                         pa_list.append(pa)
#                 cookie2 = ";".join(pa_list)
#                 cookie = ";".join([cookie1, cookie2])
#                 bools_post = await chaoxing_post_cookie(cookie)
#                 if bools_post:
#                     return_info.status = SUCCESS
#                     return_info.msg_code = 200
#                     return_info.msg = "cookie请求成功"
#                     return_info.data = {"Cookie": cookie}
#                     return return_info.todict()
#                 else:
#                     return_info.status = FAILED
#                     return_info.msg_code = MsgCode.CHAOXIN_COOKIE_ERROR
#                     return_info.msg = "请求存储API失败"
#                     return_info.data = ""
#                     return return_info.todict()
#             else:
#                 return_info.status = FAILED
#                 return_info.msg_code = MsgCode.CHAOXIN_COOKIE_ERROR
#                 return_info.msg = "代理请求二级目标网页失败"
#                 return_info.data = ""
#                 return return_info.todict()
#
#         else:
#             return_info.status = FAILED
#             return_info.msg_code = MsgCode.CHAOXIN_COOKIE_ERROR
#             return_info.msg = "代理请求一级目标网页失败"
#             return_info.data = ""
#             return return_info.todict()
